//
//  GWMeTableViewFooterView.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/8.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWMeTableViewFooterView.h"
#import <AFNetworking.h>
#import "GWSquare.h"
#import <MJExtension.h>
#import "GWSquareView.h"
#import "GWMainTabbarController.h"
#import "GWWebViewController.h"
@implementation GWMeTableViewFooterView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
       
    }
    return self;
}
-(void)setSquareArray:(NSArray *)squareArray
{
    _squareArray =squareArray;
    NSInteger maxClus =4;
    CGFloat btnW =GWScreenW/maxClus;
    CGFloat btnH =btnW;
    for (int i=0; i<squareArray.count; i++) {
        GWSquareView *squreView =[[GWSquareView alloc]init];
        GWSquare *square =squareArray[i];
        squreView.square =square;
        [squreView addTarget:self action:@selector(btnOpenWebView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:squreView];
        
        NSInteger clu =i%maxClus;
        NSInteger row =i/maxClus;
        squreView.frame =CGRectMake(clu*btnW, row*btnH, btnW, btnH);
    }
    self.height =(squareArray.count +maxClus-1)/maxClus*btnH;
    [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
    [[UIImage imageNamed:@"mainCellBackground"] drawInRect:rect];
}
-(void)btnOpenWebView:(GWSquareView *)squareView
{
    
    if (![squareView.square.url hasPrefix:@"http"]) return;
   GWMainTabbarController *tabbar = (GWMainTabbarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
   UINavigationController *vc = tabbar.selectedViewController;
    GWWebViewController *web =[[GWWebViewController alloc]init];
    web.url =squareView.square.url;
    web.title =squareView.square.name;
    [vc pushViewController:web animated:YES];
}
@end
